const { Controller } = require('egg')

class EaController extends Controller {
  /**
   * @return {Object} 登录
   */
  async login() {
    this.ctx.body = { code: 200 }
  }

  /**
   * @return {Object} 学籍信息
   */
  async sInfo() {
    try {
      const table = await this.service.sInfo.table()
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '数据获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 学期成绩
   */
  async grade() {
    try {
      const { term } = this.ctx.data
      const { table, header } = await this.service.grade.table(term || '')
      this.ctx.body = {
        code: 200,
        data: {
          table,
          header,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '成绩数据获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 成绩占比详情
   */
  async gradeProportion() {
    try {
      const { link } = this.ctx.data
      const table = await this.service.grade.proportion(link)
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '成绩占比详情获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 学期课程表
   */
  async course() {
    try {
      const { term } = this.ctx.data
      const { table, time, note } = await this.service.course.table(term)
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
          time,
          note,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 课程表--班级
   */
  async courseClass() {
    try {
      const {
        term, yx, nj, zy, index,
      } = this.ctx.data
      const table = await this.service.course.Ctable(term, yx, nj, zy, index)
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 课程表--教师
   */
  async courseTeacher() {
    try {
      const { term, yx, name } = this.ctx.data
      // console.log(term, yx, name);
      const table = await this.service.course.Ttable(term, yx, name)
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 课程表--教室
   */
  async courseRoom() {
    try {
      const { term, name } = this.ctx.data
      const table = await this.service.course.Rtable(term, name)
      // console.log('table', JSON.stringify(table, null, 2));

      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '课表获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 考试安排
   */
  async exam() {
    try {
      const { term } = this.ctx.data
      const table = await this.service.exam.table(term)
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '考试安排获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 空闲教室
   */
  async emptyRoom() {
    try {
      const table = await this.service.emptyRoom.table()
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '空闲教室获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 学分修读情况
   */
  async credit() {
    try {
      const { sid } = this.ctx.data
      const table = await this.service.credit.table(sid)
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '学分修读数据获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 第二课程学分（素拓分）
   */
  async creditExtra() {
    try {
      const table = await this.service.credit.extra()
      this.ctx.body = {
        code: 200,
        success: Boolean(table),
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '素拓分获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 执行计划/培养计划（大学修读的课程）
   */
  async plan() {
    try {
      const table = await this.service.plan.table()
      this.ctx.body = {
        code: 200,
        data: {
          table,
        },
      }
    } catch (err) {
      console.log(err)
      this.ctx.body = {
        code: 500,
        msg: '数据获取异常，请稍后重试或向客服反馈',
      }
    }
  }

  /**
   * @return {Object} 树形菜单数据获取
   */
  async tree() {
    const { type } = this.ctx.data
    let tree
    if (type === 'class') {
      tree = await this.service.tree.class_()
    }
    // else if (type === 'teacher') {
    //   tree = await this.service.tree.teacher();
    // }
    // else if (type === 'classroom') {
    //   tree = await this.service.tree.classroom();
    // }
    this.ctx.body = {
      code: 200,
      data: {
        tree,
      },
    }
  }
}

module.exports = EaController
